const adminModel = require('../models/adminModel');
const utils = require('../utils/index')

const myGlobal = {};
exports.index=(req,res)=>{ 
    res.render('admin/home')
}

exports.loginPage=(req,res)=>{
    res.render('admin/adLogin',{msg:myGlobal.msg})
}

exports.login=(req,res)=>{
    console.log('222222222222');
    console.log(req.body);
    adminModel.login(req.body).then(function (data) { 

        if(data.length>0){
            //登录成功
            req.session.user = data[0];
            res.redirect('/admin/home');
        }else{
            myGlobal.msg = '用户名或密码错误'
            res.redirect('/admin/adLogin');
        }
    });

}
// --------------------------------文章管理------------------------
exports.getAllArtical=(req,res)=>{
    myGlobal.path = req.originalUrl;
    adminModel.getAllArtical(req.query.page).then((data)=>{
        let result = {
            total:data[1][0].total,
            articals:data[0],
            path: req.originalUrl
        }
        res.render('admin/artical',result);
      
    })
}
exports.addArticalPage = (req,res)=>{
    adminModel.addArticalPage().then((data)=>{
        res.render('admin/artical_add',{path:myGlobal.path,types:data})
    }) 
}
exports.addArtical = (req,res)=>{
    req.body.userId = 2;
    console.log(req.body);
    adminModel.addArtical(req.body).then((data)=>{
        // console.log(data);
        
        if(data.affectedRows>0){
            res.redirect('/admin/artical')}
    })
}
//--------------------------用户管理模块-----------------------
exports.listUser=(req,res)=>{

    adminModel.getAllUser(req.query.page).then(function(data){

        let total=data[1][0].total
        data[0]=utils.arrFormat(data[0]);
        res.render('admin/user',{users:data[0],total:total})
    })
}

//新增：第一步
exports.addPage=(req,res)=>{
    res.render('admin/user_add')
}
//新增：第二步
exports.addUser=(req,res)=>{
    adminModel.addUser(req.body).then(function(data){
        if(data.affectedRows>0){
            res.redirect('/admin/user')
        }
    })
}

exports.deleteArtical = (req,res)=>{
    adminModel.deleteArtical(req.params.id).then((data)=>{
        if(data.affectedRows>0){
            res.redirect('/admin/artical')
        }
    })
}
exports.editArticalPage =(req,res)=>{
    // res.render('admin/artical_edit',{path:myGlobal.path})
    adminModel.editArticalPage(req.params.id).then((data)=>{
        // console.log(data);
        res.render('admin/artical_edit',{path:myGlobal.path,artical:data[0][0],types:data[1]})
        // res.send(data[1]);
    })
}
exports.editArtical= (req,res)=>{
    console.log(req.body);
    
    adminModel.editArtical(req.body).then((data)=>{

        console.log(data);
        
        if(data.affectedRows>0){
            res.redirect('/admin/artical')
        }
    })
}
//编辑第一步
exports.getOneUser=(req,res)=>{
    adminModel.getOneUser(req.params.id).then(function(data){
        // console.log(data);
        
        res.render('admin/user_edit',{editUser:data[0]})
    })
    
}

//编辑第二步
exports.editUser=(req,res)=>{
    adminModel.editUser(req.body).then(function(data){
        res.redirect('/admin/user')
    })
}

//删除
exports.deleteUser=(req,res)=>{
    adminModel.deleteUser(req.params.id).then(function(data){ 
        res.redirect('/admin/user')
    })
}
//查询
exports.searchUser=(req,res)=>{
    if(req.body.search.length==0) return res.json({result:'空数据'})
    adminModel.searchUser(req.body.search).then(function(data){

        res.json(data)
    })
}
// ---------------------菜单管理----------------
exports.menu=(req,res)=>{
    adminModel.getAllMenu().then(data=>{
          res.render('admin/menu',{menus:utils.arrFormat(data)});      
    })
}
exports.menuAdd = (req,res)=>{
    res.render('admin/menu_add');
}
exports.menuAddUser =(req,res)=>{
    adminModel.AddMenu(req.body).then(data=>{
        if(data.affectedRows>0){
            res.redirect('/admin/menu')
        }
        
    })
    
}
exports.menuEdit =(req,res)=>{
    if(req.params.id=='favicon.ico')return;
    console.log(req.params.id);
    adminModel.getOneMenu (req.params.id).then(data=>{
        console.log(data);
        res.render('admin/menu_Edit',{menu:data[0]})
        
    })
    
}
exports.menuEditUser =(req,res)=>{
    // console.log(req.body);
    adminModel.editMenu(req.body).then(data=>{
        if(data.affectedRows>0){
            res.redirect('/admin/menu')
        }
    })
    
}
exports.menuDelete =(req,res)=>{
    // console.log(req.body);
    if(req.params.id=='favicon.ico')return;
    console.log(req.params.id);
    
    adminModel.deleteMenu(req.params.id).then(data=>{
        if(data.affectedRows>0){
            res.redirect('/admin/menu')
        }
    })
    
}
// ----------------文章分类模块---------------
// router.get('/classify',adminController.classify);
// router.get('/classifyAdd',adminController.classifyAdd);
// router.post('/classifyAdd',adminController.classifyAddUser);
// router.get('/classifyEdit/:id',adminController.classifyEdit);
// router.post('/classifyEdit',adminController.classifyEditUser);
// router.get('classifyDelete/:id',adminController.classifyDelete );
exports.classify=(req,res)=>{
    adminModel.getAllClasses().then(data=>{
        if (data.length>0) {
            res.render('admin/class',{classes:utils.arrFormat(data)})
        }
    })
}
exports.classifyAdd=(req,res)=>{
    res.render('admin/class_add');
}
exports.classifyAddUser=(req,res)=>{
    console.log(req.body);
    adminModel.AddClass(req.body).then(data=>{
        if(data.affectedRows>0){
            res.redirect('/admin/classify')
        }
    })
    
}
exports.classifyEdit=(req,res)=>{
    if(req.params.id=='favicon.ico') return;
    adminModel.getOneClass(req.params.id).then(data=>{
        console.log(data);
        res.render('admin/class_edit',{aclass:data[0]})
    })
}
exports.classifyEditUser=(req,res)=>{
    console.log(req.body);
    adminModel.editClass(req.body).then(data=>{
        if(data.affectedRows>0){
            res.redirect('/admin/classify');
        }
    })
    
    
}
exports.classifyDelete =(req,res)=>{
    if(req.params.id=='favicon.ico') return;
    console.log(req.params.id);
    
    adminModel.deleteClass(req.params.id).then(data=>{
        if(data.affectedRows>0){
            res.redirect('/admin/classify');
        }
    })
    
}